package com.module.base.widget.skin.s

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.CompoundButton
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import skin.support.widget.SkinCompatBackgroundHelper
import skin.support.widget.SkinCompatCompoundButtonHelper
import skin.support.widget.SkinCompatImageHelper
import skin.support.widget.SkinCompatTextHelper

/**
 * Author: yanxuwen
 * Date: 2023/11/17
 * Desc: 系统的皮肤
 */
class SkinCompatImpl(var view: View) {

    //系统的文字皮肤
    private var mTextHelper: SkinCompatTextHelper? = null

    //系统的背景皮肤
    private var mBackgroundTintHelper: SkinCompatBackgroundHelper? = null

    //系统图片皮肤（抄SkinCompatImageView）
    private var mImageHelper: SkinCompatImageHelper? = null

    private var mCompoundButtonHelper: SkinCompatCompoundButtonHelper? = null


    fun initTypedArray(attrs: AttributeSet?, defStyleAttr: Int) {
        mBackgroundTintHelper = SkinCompatBackgroundHelper(view)
        mBackgroundTintHelper?.loadFromAttributes(attrs, defStyleAttr)
        when (view) {
            is TextView -> {
                mTextHelper = SkinCompatTextHelper.create(view as TextView)
                mTextHelper?.loadFromAttributes(attrs, defStyleAttr)
            }
            is ImageView -> {
                mImageHelper = SkinCompatImageHelper(view as ImageView)
                mImageHelper?.loadFromAttributes(attrs, defStyleAttr)
            }
            is CompoundButton -> {
                mCompoundButtonHelper = SkinCompatCompoundButtonHelper(view as CompoundButton)
                //尼玛，怎么是私有的，先暂时隐藏
    //            mCompoundButtonHelper?.loadFromAttributes(attrs, defStyleAttr)
            }
        }
    }

    fun setBackgroundResource(@DrawableRes resId: Int) {
        mBackgroundTintHelper?.onSetBackgroundResource(resId)
    }

    fun setTextAppearance(resId: Int) {
        setTextAppearance(view.context, resId)
    }

    fun setTextAppearance(context: Context?, resId: Int) {
        mTextHelper?.onSetTextAppearance(context, resId)
    }

    fun setCompoundDrawablesRelativeWithIntrinsicBounds(
        @DrawableRes start: Int, @DrawableRes top: Int, @DrawableRes end: Int, @DrawableRes bottom: Int
    ) {
        mTextHelper?.onSetCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)
    }

    fun setCompoundDrawablesWithIntrinsicBounds(
        @DrawableRes left: Int, @DrawableRes top: Int, @DrawableRes right: Int, @DrawableRes bottom: Int
    ) {
        mTextHelper?.onSetCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
    }

    fun setImageResource(@DrawableRes resId: Int) {
        mImageHelper?.setImageResource(resId)
    }

    fun setButtonDrawable(@DrawableRes resId: Int) {
        mCompoundButtonHelper?.setButtonDrawable(resId)
    }

    fun applySkin() {
        mBackgroundTintHelper?.applySkin()
        mTextHelper?.applySkin()
        mImageHelper?.applySkin()
        mCompoundButtonHelper?.applySkin()
    }
}